package cn.com.member.controller.ping;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pingplusplus.Pingpp;
import com.pingplusplus.model.Charge;

import cn.com.alipay.util.AlipayNotify;
import cn.com.api.entity.ApiTmember;
import cn.com.api.service.ApiPaymentService;
import cn.com.api.service.ApiT_orderService;
import cn.com.pingpp.PingppConfig;
import cn.com.util.*;

@Controller
public class ApiPaymentController {
	@Autowired
	private ApiPaymentService apiPaymentService;
	@Autowired
	private ApiT_orderService apiT_orderService;
	private static Logger logger = Logger.getLogger(ApiPaymentController.class);

	/**
	 * Ping++ 获取支付凭证(chareg对象)
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "getPingppCharge")
	@ResponseBody
	public Charge getPingppCharge(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			String order_no = request.getParameter("order_no");
			Integer amount = Integer.valueOf(request.getParameter("amount"));
			String channel = request.getParameter("channel");
			String client_ip = request.getParameter("client_ip");
			String subject = request.getParameter("subject");
			String body = request.getParameter("order_desc");
			if (PingppConfig.pingChanelmap.containsKey(channel)) {

				Map<String, String> extra = new HashMap<String, String>();
				// 加入订单金额验证逻辑
				// ?????????
				Pingpp.apiKey = PingppConfig.apiKey;
				Map<String, Object> chargeParams = new HashMap<String, Object>();
				Map<String, String> app = new HashMap<String, String>();

				switch (channel) {
				case "alipay_pc_direct":
					chargeParams.put("order_no", order_no);
					chargeParams.put("amount", amount);
					app.put("id", PingppConfig.appId);
					chargeParams.put("app", app);
					chargeParams.put("channel", "alipay_pc_direct");
					chargeParams.put("currency", "cny");
					chargeParams.put("client_ip", client_ip);
					chargeParams.put("subject", subject);
					chargeParams.put("body", body);
					extra.put("success_url", "http://www.sm1168.com/");
					chargeParams.put("extra", extra);
					break;
				case "wx_pub_qr":
					chargeParams.put("order_no", order_no);
					chargeParams.put("amount", amount);
					app.put("id", PingppConfig.appId);
					chargeParams.put("app", app);
					chargeParams.put("channel", "wx_pub_qr");
					chargeParams.put("currency", "cny");
					chargeParams.put("client_ip", client_ip);
					chargeParams.put("subject", subject);
					chargeParams.put("body", body);
					extra.put("product_id", Utils.getDingDanNo(""));
					chargeParams.put("extra", extra);
					break;
				default:
					break;
				}
				Charge charge = Charge.create(chargeParams);
				return charge;
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
		}
		return null;
	}

	/**
	 * 余额支付
	 **/
	@RequestMapping("usermoneypay")
	public void usermoneypay(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 订单编号
		String order_num = request.getParameter("order_num");
		String paypwd = request.getParameter("paypwd");
		String order_money = request.getParameter("order_money");
		BigDecimal amount = new BigDecimal(order_money).divide(new BigDecimal(100));
		ApiTmember apiTmember = (ApiTmember) request.getSession().getAttribute("webuser");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = apiT_orderService.usermoneypay(order_num, paypwd, apiTmember.getCid(), amount);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统错误");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
}
